package com.example.JcGQS.dao;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.example.JcGQS.enums.PurchaseStateEnum;
import com.example.JcGQS.mapper.PurchaseMapper;
import com.example.JcGQS.pojo.dto.PageADTO;
import com.example.JcGQS.pojo.dto.PurchaseId;
import com.example.JcGQS.pojo.entity.Purchase;

import com.example.JcGQS.pojo.vo.PurchaseVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * @author 末日
 * @ClassName PurchaseDao
 * @Description 采购信息dao
 * @date 2023/11/21
 */
@Component
public class PurchaseDao {

    @Autowired
    private PurchaseMapper purchaseMapper;


    /**
     * 新增采购信息
     */
    public void insertPurchase(Purchase Purchase) {
        purchaseMapper.insert(Purchase);
    }

    /**
     * 删除采购信息
     */
    public void deletePurchase(Integer purchaseId) {
        purchaseMapper.deleteById(purchaseId);
    }

    /**
     * 根据采购编号查询采购信息
     *
     * @param order
     * @return
     */
    public Purchase PurchaseOrder(String order) {
        QueryWrapper<Purchase> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("purchase_order", order);
        queryWrapper.orderByAsc("purchase_id");
        return purchaseMapper.selectOne(queryWrapper);
    }

    /**
     * 查询采购信息列表
     */
    public List<Purchase> PurchaseList() {
        QueryWrapper<Purchase> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByAsc("purchase_id");
        return purchaseMapper.selectList(queryWrapper);
    }

    /**
     * 根据申购人查询(审核中/已下单/采购中/采购完成/退货中)采购信息
     */
    public List<PurchaseVo> applicantReviewedingPurchase(Integer userId) {
//        ValueQo valueQo = new ValueQo();
//        valueQo.setValue1(PurchaseStateEnum.REVIEWEDING.getValue());
//        valueQo.setValue2(PurchaseStateEnum.HAVE_ORDER.getValue());
//        valueQo.setValue3(PurchaseStateEnum.PURCHASING.getValue());
//        valueQo.setValue4(PurchaseStateEnum.PURCHASE_SUCCESS.getValue());
//        valueQo.setValue5(PurchaseStateEnum.ON_RETURN.getValue());
        List<String> list = new ArrayList<>();
        list.add(PurchaseStateEnum.REVIEWEDING.getValue());
        list.add(PurchaseStateEnum.HAVE_ORDER.getValue());
        list.add(PurchaseStateEnum.PURCHASING.getValue());
        list.add(PurchaseStateEnum.PURCHASE_SUCCESS.getValue());
        list.add(PurchaseStateEnum.ON_RETURN.getValue());
        return purchaseMapper.getApplicantReviewedingPurchase("1", "0", userId, list);
    }

    /**
     * 根据申购人查询(采购失败并删除/审核失败并删除)采购信息
     */
    public List<PurchaseVo> applicantDeletePurchase(Integer userId) {
//        ValueQo valueQo = new ValueQo();
//        valueQo.setValue1(PurchaseStateEnum.NOT_ALLOW_DELETE.getValue());
//        valueQo.setValue2(PurchaseStateEnum.PURCHASE_FAILURE_DELETE.getValue());
        List<String> list = new ArrayList<>();
        list.add(PurchaseStateEnum.NOT_ALLOW_DELETE.getValue());
        list.add(PurchaseStateEnum.PURCHASE_FAILURE_DELETE.getValue());
        return purchaseMapper.getApplicantReviewedingPurchase("1", "1", userId, list);
    }

    /**
     * 根据申购人查询(待提交)采购信息
     */
    public List<PurchaseVo> applicantToSubmitPurchase(Integer userId) {
//        ValueQo valueQo = new ValueQo();
//        valueQo.setValue1(PurchaseStateEnum.TO_BE_SUBMITTED.getValue());
        List<String> list = new ArrayList<>();
        list.add(PurchaseStateEnum.TO_BE_SUBMITTED.getValue());
        return purchaseMapper.getApplicantReviewedingPurchase("1", "0", userId, list);
    }

    /**
     * 根据申购人查询(采购失败/审核失败)采购信息
     */
    public List<PurchaseVo> applicantToSubmitPurchaseLose(Integer userId) {
//        ValueQo valueQo = new ValueQo();
//        valueQo.setValue1(PurchaseStateEnum.NOT_ALLOW.getValue());
//        valueQo.setValue2(PurchaseStateEnum.PURCHASE_FAILURE.getValue());
        List<String> list = new ArrayList<>();
        list.add(PurchaseStateEnum.NOT_ALLOW.getValue());
        list.add(PurchaseStateEnum.PURCHASE_FAILURE.getValue());
        return purchaseMapper.getApplicantReviewedingPurchase("1", "0", userId, list);
    }

    /**
     * 根据审核人查询(审核中)采购信息
     */
    public List<PurchaseVo> auditReviewedingPurchase(Integer userId) {
//        ValueQo valueQo = new ValueQo();
//        valueQo.setValue1(PurchaseStateEnum.REVIEWEDING.getValue());
        List<String> list = new ArrayList<>();
        list.add(PurchaseStateEnum.REVIEWEDING.getValue());
        return purchaseMapper.getApplicantReviewedingPurchasess("0", userId, list);
    }

    /**
     * 根据审核人查询(审核失败/采购中/已下单/采购完成/采购失败/采购失败并删除/审核失败并删除/退货中)采购信息
     */
    public List<PurchaseVo> auditUnderPurchasePurchase(Integer userId) {
//        ValueQo valueQo = new ValueQo();
//        valueQo.setValue1(PurchaseStateEnum.PURCHASING.getValue());
//        valueQo.setValue2(PurchaseStateEnum.HAVE_ORDER.getValue());
//        valueQo.setValue3(PurchaseStateEnum.PURCHASE_SUCCESS.getValue());
//        valueQo.setValue4(PurchaseStateEnum.NOT_ALLOW.getValue());
//        valueQo.setValue5(PurchaseStateEnum.PURCHASE_FAILURE.getValue());
//        valueQo.setValue6(PurchaseStateEnum.NOT_ALLOW_DELETE.getValue());
//        valueQo.setValue7(PurchaseStateEnum.PURCHASE_FAILURE_DELETE.getValue());
//        valueQo.setValue8(PurchaseStateEnum.ON_RETURN.getValue());
        List<String> list = new ArrayList<>();
        list.add(PurchaseStateEnum.PURCHASING.getValue());
        list.add(PurchaseStateEnum.HAVE_ORDER.getValue());
        list.add(PurchaseStateEnum.PURCHASE_SUCCESS.getValue());
        list.add(PurchaseStateEnum.NOT_ALLOW.getValue());
        list.add(PurchaseStateEnum.PURCHASE_FAILURE.getValue());
        list.add(PurchaseStateEnum.NOT_ALLOW_DELETE.getValue());
        list.add(PurchaseStateEnum.PURCHASE_FAILURE_DELETE.getValue());
        list.add(PurchaseStateEnum.ON_RETURN.getValue());
        return purchaseMapper.getApplicantReviewedingPurchases(userId, list);
    }

    /**
     * 根据采购人来查询(审核中)采购信息
     */
    public List<PurchaseVo> buyerReviewedingPurchase(Integer userId) {
//        ValueQo valueQo = new ValueQo();
//        valueQo.setValue1(PurchaseStateEnum.REVIEWEDING.getValue());
        List<String> list = new ArrayList<>();
        list.add(PurchaseStateEnum.REVIEWEDING.getValue());

        return purchaseMapper.getApplicantReviewedingPurchase("2", "0", userId, list);
    }

    /**
     * 根据采购人来查询(采购中/已下单)采购信息
     */
    public List<PurchaseVo> buyerUnderPurchasePurchase(Integer userId) {
//        ValueQo valueQo = new ValueQo();
//        valueQo.setValue1(PurchaseStateEnum.PURCHASING.getValue());
//        valueQo.setValue2(PurchaseStateEnum.HAVE_ORDER.getValue());
        List<String> list = new ArrayList<>();
        list.add(PurchaseStateEnum.PURCHASING.getValue());
        list.add(PurchaseStateEnum.HAVE_ORDER.getValue());

        return purchaseMapper.getApplicantReviewedingPurchase("2", "0", userId, list);
    }

    /**
     * 根据采购人来查询(采购完成)采购信息
     */
    public List<PurchaseVo> buyerPurchaseCompletedPurchase(Integer userId) {
//        ValueQo valueQo = new ValueQo();
//        valueQo.setValue1(PurchaseStateEnum.PURCHASE_SUCCESS.getValue());
        List<String> list = new ArrayList<>();
        list.add(PurchaseStateEnum.PURCHASE_SUCCESS.getValue());

        return purchaseMapper.getApplicantReviewedingPurchase("2", "0", userId, list);
    }

    /**
     * 根据采购人来查询(采购失败/采购失败并删除/退货中)采购信息
     */
    public List<PurchaseVo> buyerPurchaseFailedPurchase(Integer userId) {
//        ValueQo valueQo = new ValueQo();
//        valueQo.setValue1(PurchaseStateEnum.PURCHASE_FAILURE.getValue());
//        valueQo.setValue2(PurchaseStateEnum.PURCHASE_FAILURE_DELETE.getValue());
//        valueQo.setValue3(PurchaseStateEnum.ON_RETURN.getValue());
        List<String> list = new ArrayList<>();
        list.add(PurchaseStateEnum.PURCHASE_FAILURE.getValue());
        list.add(PurchaseStateEnum.PURCHASE_FAILURE_DELETE.getValue());
        list.add(PurchaseStateEnum.ON_RETURN.getValue());
        return purchaseMapper.getApplicantReviewedingPurchas("2", userId, list);
    }

    /**
     * 根据仓库管理人来查询(已下单)的采购信息
     */
    public List<PurchaseVo> warehousemanHaveOrderPurchase() {
        List<String> list = new ArrayList<>();
        list.add(PurchaseStateEnum.HAVE_ORDER.getValue());
        return purchaseMapper.getApplicantReviewedingPurchasesing("0", list);
    }


    /**
     * 根据仓库管理人来查询(退货中)的采购信息
     */
    public List<PurchaseVo> warehousemanReturnPurchase() {
        /*ValueQo valueQo = new ValueQo();
        valueQo.setValue1(PurchaseStateEnum.ON_RETURN.getValue());*/
        List<String> list = new ArrayList<>();
        list.add(PurchaseStateEnum.ON_RETURN.getValue());
        return purchaseMapper.getApplicantReviewedingPurchasesing("0", list);
    }

    /**
     * 根据仓库管理人来查询(采购完成)的采购信息
     */
    public List<PurchaseVo> warehousemanPurchaseCompletedPurchase() {
       /* ValueQo valueQo = new ValueQo();
        valueQo.setValue1(PurchaseStateEnum.PURCHASE_SUCCESS.getValue());*/
        List<String> list = new ArrayList<>();
        list.add(PurchaseStateEnum.PURCHASE_SUCCESS.getValue());
        return purchaseMapper.getApplicantReviewedingPurchasesing("0", list);
    }

    /**
     * 根据仓库管理人来查询(采购失败/采购失败并删除)的采购信息
     */
    public List<PurchaseVo> warehousemanPurchaseFailedPurchase() {
//        ValueQo valueQo = new ValueQo();
//        valueQo.setValue1(PurchaseStateEnum.PURCHASE_FAILURE.getValue());
//        valueQo.setValue2(PurchaseStateEnum.PURCHASE_FAILURE_DELETE.getValue());
        List<String> list = new ArrayList<>();
        list.add(PurchaseStateEnum.PURCHASE_FAILURE.getValue());
        list.add(PurchaseStateEnum.PURCHASE_FAILURE_DELETE.getValue());
        return purchaseMapper.getwarehousemanPurchaseFailedPurchase(list);
    }
//    /**
//     * 根据
//     */
//    public List<PurchaseVo> a(PageADTO pageADTO,Integer userId){
//        ValueQo valueQo = new ValueQo();
//        List<String> states = pageADTO.getStates();
//        for (int i = 0;i<states.size();i++) {
//            String state = states.get(i);
//
//            if (i == 0) {
//                valueQo.setValue1(state);
//            } else if (i == 1) {
//                valueQo.setValue2(state);
//            } else if (i == 2) {
//                valueQo.setValue3(state);
//            } else if (i == 3) {
//                valueQo.setValue4(state);
//            } else if (i == 4) {
//                valueQo.setValue5(state);
//            } else if (i == 5) {
//                valueQo.setValue6(state);
//            } else if (i == 6) {
//                valueQo.setValue7(state);
//            } else if (i == 7) {
//                valueQo.setValue8(state);
//            } else if (i == 8) {
//                valueQo.setValue9(state);
//            } else if (i == 9) {
//                valueQo.setValue10(state);
//            } else if (i == 10) {
//                valueQo.setValue11(state);
//            }
//        }
//        return purchaseMapper.a(pageADTO.getValue(),userId, valueQo);
//    }

    /**
     * 根据id查询采购信息数据
     */
    public Integer PurchaseSelect(Integer id) {
        QueryWrapper<Purchase> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("purchase_id", id);
        return purchaseMapper.selectCount(queryWrapper);
    }

    /**
     * 根据id查询采购信息数据
     */
    public Purchase PurchaseSelectId(Integer id) {
        QueryWrapper<Purchase> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("purchase_id", id);
        return purchaseMapper.selectOne(queryWrapper);
    }


    /**
     * 更新采购信息
     *
     * @param purchase 货物
     */
    public void updatePurchase(Purchase purchase) {
        UpdateWrapper<Purchase> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("purchase_id", purchase.getPurchaseId());
        purchaseMapper.update(purchase, updateWrapper);
    }

    /**
     * 更新采购信息
     */
    public void updatePurchaseId(Integer id, String state) {
        Purchase purchase = new Purchase();
        purchase.setPurchaseId(id);
        purchase.setPurchaseState(state);
        if (state.equals(PurchaseStateEnum.PURCHASING.getValue())) {
            purchase.setPurchaseReviewTime(LocalDateTime.now());
        }
        purchaseMapper.updateById(purchase);
    }

}
